package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.common.redis.annotation.XingXingCache;
import com.spzx.product.api.domain.SkuPrice;
import com.spzx.product.api.domain.SkuQuery;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.mapper.ProductSkuMapper;
import com.spzx.product.service.ProductSkuService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ProductSkuServiceImpl
 * @Author 波
 * @Date 2024/7/30 16:13
 */
@Service
public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, ProductSku> implements ProductSkuService {
    /**
     * 获取销量最好的sku
     *
     * @return List<ProductSku>
     */
    @Override
    @XingXingCache(prefix = "product:sku:topSale")
    public List<ProductSku> getTopSale() {
        return baseMapper.getTopSale();
    }

    /**
     * 查询商品列表
     *
     * @param skuQuery 查询的条件
     * @return List<ProductSku>
     */
    @Override
    @XingXingCache(prefix = "product:sku:list")
    public List<ProductSku> getProductSkuList(SkuQuery skuQuery) {
        return baseMapper.getProductSkuList(skuQuery);
    }

    /**
     * 获取skuPrice信息
     *
     * @param skuId
     * @return
     */
    @XingXingCache(prefix = "product:sku:price")
    @Override
    public SkuPrice getSkuPrice(Long skuId) {
        LambdaQueryWrapper<ProductSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductSku::getId, skuId);
        ProductSku productSku = baseMapper.selectOne(queryWrapper);
        SkuPrice skuPrice = new SkuPrice();
        BeanUtils.copyProperties(productSku, skuPrice);
        skuPrice.setSkuId(skuId);
        return skuPrice;
    }

    /**
     * 获取SkuSpecValueMap信息
     *
     * @param pid pid
     * @return
     */
    @Override
    @XingXingCache(prefix = "product:sku:specValue")
    public Map<String, Long> getSkuSpecValueMap(Long pid) {

        List<ProductSku> productSkuList = baseMapper.selectList(new LambdaQueryWrapper<ProductSku>().eq(ProductSku::getProductId, pid
        ).select(ProductSku::getId, ProductSku::getSkuSpec));
        Map<String, Long> skuSpecValueMap = new HashMap<>();
        productSkuList.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(), item.getId());
        });
        return skuSpecValueMap;
    }

    @Override
    public List<SkuPrice> getSkuPriceList(List<Long> skuIdList) {
        List<ProductSku> productSkuList = baseMapper.selectList(new LambdaQueryWrapper<ProductSku>().in(ProductSku::getId, skuIdList).select(ProductSku::getId, ProductSku::getSalePrice));
        return productSkuList.stream().map(productSku -> {
            SkuPrice skuPrice = new SkuPrice();
            skuPrice.setSkuId(productSku.getId());
            skuPrice.setSalePrice(productSku.getSalePrice());
            return skuPrice;
        }).collect(Collectors.toList());
    }
}
